﻿namespace VividKit.ComponentModel
{
	using System;

	internal class CommandManager : ICommandManager, ICleanup
	{
		private readonly IDispatcher dispatcher;

		internal CommandManager(IDispatcher dispatcher)
		{
			this.dispatcher = dispatcher;
		}

		public event EventHandler RequerySuggested;

		public async void InvalidateRequerySuggested()
		{
			await this.dispatcher.InvokeAsync(InvokeEventHandlers);
		}

		void ICleanup.Cleanup()
		{
			this.RequerySuggested = null;
		}

		private void InvokeEventHandlers()
		{
			OnRequerySuggested(EventArgs.Empty);
		}

		private void OnRequerySuggested(EventArgs e)
		{
			var requerySuggested = RequerySuggested;
			if (requerySuggested != null)
			{
				requerySuggested(null, e);
			}
		}
	}
}